package com.example.myworkers.service.imp;/*
 *编写者：陆铭
 */

import com.example.myworkers.dao.IMenu;
import com.example.myworkers.model.Menu;
import com.example.myworkers.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IMenuServiceImpl implements IMenuService {
    @Autowired
    private IMenu menuMapper;

    //增加新菜单
    @Override
    public int insertMenu(Menu menu){
        int menuValue=0;
        Menu menu1=new Menu();
        menu1.setName(menu.getName());
        List<Menu> getM=menuMapper.getAllMenu(menu1);
        if(getM.size()>=1){
            menuValue=-1;
        }else {
            menuValue=menuMapper.insertMenu(menu);
        }
        return menuValue;
    }
    //查找全部菜单
    @Override
    public List<Menu> getAllMenu(Menu menu){
        return menuMapper.getAllMenu(menu);
    }

    //修改菜单
    @Override
    public int updateMenu(Menu menu){
        int menuValue=0;
        Menu menu1=new Menu();
        menu1.setName(menu.getName());
        if(menu.getId()!=0){
            if(menu.getId()>=1){
                if(menu1.getName()!=menu.getName()){
                    menuValue=-1;
                }
            }
            menuValue=menuMapper.updateMenu(menu);
        }
        return menuValue;
    }

    //删除菜单
    @Override
    public int deleteMenu(Menu menu){
        int menuValue=0;
        if(menu.getId()!=0){
            menuValue=menuMapper.deleteMenu(menu);
        }
        return menuValue;
    }
    @Override
    public Menu getMenu(Menu menu){
        Menu menu1=new Menu();
        if(menu.getId()!=0) {
        menu1=menuMapper.getMenu(menu);
        }
        return menu1;
    }

}
